package com.mall507.fivepeaks.activity.mine;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.MineCollection;
import com.mall507.comn.model.MineMessage;
import com.mall507.comn.model.ShopItem;
import com.mall507.comn.model.Tour;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static android.widget.Toast.LENGTH_LONG;

/**
 * @author
 * @version 1.0
 * @date 2019/8/26
 */
@Route(path = "/mine/CommentRUI")
public class CommentRUI extends AbsRecyclerViewUI<MineCollection> {
    public final int ITEM_TYPE_TOUR = 0xff04;//旅游
    public final int ITEM_TYPE_SPECIALTY = 0xff05;//特产
    public final int ITEM_TYPE_BUSINESS = 0xff06;//商圈
    public final int ITEM_TYPE_CULTURE = 0xff07;//文化艺术
    public final int ITEM_TYPE_ORIGINAL = 0xff08;//原创
    public final int ITEM_TYPE_DYNAMIC = 0xff09;//文章
    public final int ITEM_TYPE_ACTIVITY = 0xff10;//活动
    
    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_mine_comment_index;
    }

    @Override
    protected void bindView() {
        showLoding();
        getTitleView().setContent("我的收藏");
    }
    
    @Override
    protected String setHttpPath() {
        return "Me/getCollection";
    }

    @Override
    protected List<MineCollection> handleHttpData(JSONObject response) {
        List<MineCollection> list = new ArrayList<>();
        if(200==response.optInt("code")){
            JSONArray jsonArray=response.optJSONArray("data");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject object=jsonArray.optJSONObject(i);
                MineCollection model=new MineCollection();
                JSONArray imgs=object.optJSONArray("imgs");
                if(imgs!=null && imgs.length()>0){
                    model.setImage(imgs.optString(0));
                }else{
                    model.setImage("");
                }
                model.setType(object.optString("type"));
                model.setLvid(object.optString("id"));
                model.setTitle(object.optString("title"));
                model.setAddress(object.optString("address"));
                model.setViews(object.optString("views"));
                model.setCollection_count(object.optString("collection_count"));
                model.setComment_count(object.optString("comment_count"));

                model.setContent(object.optString("content"));
                model.setThumb(object.optString("thumb"));

                model.setBid(object.optString("bid"));
                model.setCompname(object.optString("compname"));
                model.setCompaddress(object.optString("compaddress"));
                
                model.setCover_img(object.optString("cover_img"));
                model.setHead_img(object.optString("head_img"));
                model.setNickname(object.optString("nickname"));
                model.setCreate_at(object.optString("create_at"));

                if (imgs != null && imgs.length()>0){
                    for (int j = 0;j<imgs.length();j++){
                        switch (j){
                            case 0:
                                model.setIcon1(imgs.optString(j));
                                break;
                            case 1:
                                model.setIcon2(imgs.optString(j));
                                break;
                            case 2:
                                model.setIcon3(imgs.optString(j));
                                break;
                        }
                    }
                }
                model.setName(object.optString("name"));
                model.setActivity_number(object.optString("activity_number"));
                model.setActivity_time(object.optString("activity_time"));
                model.setActivity_address(object.optString("activity_address"));
                list.add(model);
            }
        }
        dismissLoding();
        return list;
    }

    @Override
    protected int setItemViewType(int position) {
        String type = getAdapter().getDatalist().get(position).getType();
        if (type.equals("1")) {
            return ITEM_TYPE_TOUR;
        } else if (type.equals("2")) {
            return ITEM_TYPE_SPECIALTY;
        }else if (type.equals("3")){
            return ITEM_TYPE_BUSINESS;
        }else if (type.equals("4")){
            return ITEM_TYPE_CULTURE;
        }else if (type.equals("5")){
            return ITEM_TYPE_ORIGINAL;
        }else if (type.equals("6")){
            return ITEM_TYPE_ACTIVITY;
        }else if (type.equals("7")){
            return ITEM_TYPE_DYNAMIC;
        }
        return ITEM_TYPE_TOUR;
    }
    
    @Override
    protected int setItemLayoutId(int viewType) {
        if (viewType == ITEM_TYPE_TOUR) {
            return R.layout.item_rv_main_tour;
        } else if (viewType == ITEM_TYPE_SPECIALTY) {
            return R.layout.item_rv_main_specialty;
        }else if (viewType == ITEM_TYPE_BUSINESS) {
            return R.layout.item_rv_community_business;
        }else if (viewType == ITEM_TYPE_ORIGINAL) {
            return R.layout.item_rv_main_original;
        }else if (viewType == ITEM_TYPE_CULTURE) {
            return R.layout.item_rv_community_art;
        }else if (viewType == ITEM_TYPE_DYNAMIC) {
            return R.layout.item_rv_community_association_dynamic_info;
        }else if (viewType == ITEM_TYPE_ACTIVITY) {
            return R.layout.item_rv_community_association_activity_info;
        }
        return R.layout.item_rv_main_tour;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, final MineCollection model) {
        switch (getAdapter().getItemViewType(position)) {
            case ITEM_TYPE_TOUR:
                bindTour(holder, position, model);
                break;
            case ITEM_TYPE_SPECIALTY:
                bindSpecialty(holder, position, model);
                break;
            case ITEM_TYPE_BUSINESS:
                bindBusiness(holder, position, model);
                break;
            case ITEM_TYPE_ORIGINAL:
                bindOriginal(holder, position, model);
                break;
            case ITEM_TYPE_CULTURE:
                bindCulture(holder, position, model);
                break;
            case ITEM_TYPE_DYNAMIC:
                bindDynamic(holder, position, model);
                break;
            case ITEM_TYPE_ACTIVITY:
                bindActivity(holder, position, model);
                break;
        }
    }

    public void bindTour(RecyclerViewHolder holder, int position, final MineCollection model) {
        ((ExpandImageView) holder.getView(R.id.image)).setImageURI(model.getImage());
        holder.setText(R.id.title, model.getTitle());
//        holder.setText(R.id.address, model.getAddress());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.address, String.valueOf(Html.fromHtml(model.getAddress(),Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.address, String.valueOf(Html.fromHtml(model.getAddress())));
        }
        holder.setText(R.id.browseNumber, model.getViews() + "浏览");
        holder.setText(R.id.collectionNumber, model.getCollection_count() + "收藏");
        holder.setText(R.id.commentNumber, model.getComment_count() + "评论");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/main/MainTourInfoRUI")
                        .withString("id", model.getLvid())
                        .withString("type","1")
                        .withString("collection_count",model.getCollection_count())
                        .navigation();
            }
        });
    }

    public void bindSpecialty(RecyclerViewHolder holder, int position, final MineCollection model) {
        ((ExpandImageView) holder.getView(R.id.icon)).setImageURI(model.getThumb());
        holder.setText(R.id.title, model.getTitle());
        String content = model.getContent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.desc, String.valueOf(Html.fromHtml(content,Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.desc, String.valueOf(Html.fromHtml(content)));
        }
//        holder.setText(R.id.desc, content);
        holder.setText(R.id.collectionNumber, model.getCollection_count() + "收藏");
        holder.setText(R.id.commentNumber, model.getComment_count() + "评论");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/main/MainSpecialtyInfoRUI")
                        .withString("id", model.getLvid())
                        .withString("type","2")
                        .withString("collection_count",model.getCollection_count())
                        .navigation();
            }
        });
    }

    public void bindBusiness(RecyclerViewHolder holder, int position, final MineCollection model) {
        ((ExpandImageView) holder.getView(R.id.icon)).setImageURI(model.getThumb());
        holder.setText(R.id.title, model.getCompname());
        holder.setText(R.id.desc, "地址：" + model.getCompaddress());
        holder.setText(R.id.collectionNumber,model.getCollection_count() + "收藏");
        holder.setText(R.id.commentNumber,model.getComment_count() + "评论");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/community/CommunityBusinessStoreInfoRUI")
                        .withString("id", model.getBid())
                        .withString("type","3")
                        .withString("collection_count",model.getCollection_count())
                        .navigation();
            }
        });
    }

    public void bindOriginal(RecyclerViewHolder holder, int position, final MineCollection model) {
        ((ExpandImageView) holder.getView(R.id.image)).setImageURI(model.getCover_img());
        holder.setText(R.id.title,model.getTitle());
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getHead_img());
        holder.setText(R.id.nice,model.getNickname());
        holder.setText(R.id.time,model.getCreate_at());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.content, String.valueOf(Html.fromHtml(model.getContent(),Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.content, String.valueOf(Html.fromHtml(model.getContent())));
        }
//        holder.setText(R.id.content,model.getContent());
        holder.setText(R.id.browseNumber,model.getViews()+"人浏览");
        holder.setText(R.id.lickNumber,model.getCollection_count());
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build("/comn/ComnCultureItemInfoRUI")
                        .withString("type","5")
                        .withString("collection_count","5")
                        .withString("id", model.getLvid())
                        .withString("commentRequestPath","Comment/getOriginalComment")
                        .withString("headRequestPath","Original/getOriginDetail")
                        .navigation();
            }
        });
    }

    public void bindCulture(RecyclerViewHolder holder, int position, final MineCollection model) {

        ((ExpandImageView) holder.getView(R.id.image)).setImageURI(model.getCover_img());
        holder.setText(R.id.title, model.getTitle());
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getHead_img());
        holder.setText(R.id.nice, model.getNickname());
        holder.setText(R.id.time, model.getCreate_at());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.content, String.valueOf(Html.fromHtml(model.getContent(),Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.content, String.valueOf(Html.fromHtml(model.getContent())));
        }
        holder.setText(R.id.content, model.getContent());
        holder.setText(R.id.browseNumber, model.getViews() + "人浏览");
        holder.setText(R.id.lickNumber, model.getCollection_count());
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build("/comn/ComnCultureItemInfoRUI")
                        .withString("type","4")
                        .withString("collection_count",model.getCollection_count())
                        .withString("id", model.getLvid())
                        .withString("commentRequestPath","Comment/getArtComment")
                        .withString("headRequestPath","Community/getArtBlog")
                        .navigation();
            }
        });
    }

    public void bindDynamic(RecyclerViewHolder holder, int position, final MineCollection model) {
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getHead_img());
        holder.setText(R.id.nice, model.getName() + "  " + model.getCreate_at());
        holder.setText(R.id.name, model.getTitle());
//        holder.setText(R.id.time, model.getCreate_at());
        holder.setText(R.id.pageview, model.getViews() + "人浏览");
        ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
        ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
        ((ExpandImageView) holder.getView(R.id.image3)).setImageURI(model.getIcon3());
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/comn/ComnArticleItemInfoRUI")
                        .withString("type","7")
                        .withString("collection_count",model.getCollection_count())
                        .withString("id", model.getLvid())
                        .withString("commentRequestPath","Comment/getAssociationComment")
                        .withString("headRequestPath","Association/getAssociationActivity")
                        .navigation();
            }
        });
    }

    public void bindActivity(RecyclerViewHolder holder, int position, final MineCollection model) {
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getHead_img());
        holder.setText(R.id.nice, model.getName());
        holder.setText(R.id.name, model.getTitle());
        holder.setText(R.id.time, model.getCreate_at());
        holder.setText(R.id.pageview, model.getViews() + "人浏览");
        ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getCover_img());
        holder.setText(R.id.child_number, "人数：" + model.getActivity_number());
        holder.setText(R.id.child_time, "时间：" + model.getActivity_time());
        holder.setText(R.id.child_address, "地点：" + model.getActivity_address());
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/community/CommunityAssociationInfoActivityRUI")
                        .withString("type","6")
                        .withString("collection_count",model.getCollection_count())
                        .withString("id", model.getLvid())
                        .navigation();
            }
        });
    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }
}
